package com.zjj.xiancheng.sync.account;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class Account {

    private String name;
    private double balance;
    private final Lock lock = new ReentrantLock();

    public Account() {
    }

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

    public Account withdraw(double money) {
        try{
            lock.lock();
            //取款逻辑
            if(this.balance>=money){
                System.out.println("账户余额："+this.balance+" , "+Thread.currentThread().getName()+"正在取钱");
                this.balance -= money;
                System.out.println("当前账户剩余存款："+this.balance);
            }else{
                System.out.println(Thread.currentThread().getName()+"取钱，但是余额不足！");
                System.out.println("当前账户剩余存款："+this.balance);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        return this;
    }


    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * 设置
     * @param balance
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String toString() {
        return "Account{name = " + name + ", balance = " + balance + ", lock = " + lock + "}";
    }
}
